package com.ruoyi.statistics.service.impl;

import com.ruoyi.statistics.domain.StoreRank;
import com.ruoyi.statistics.mapper.SysStatisticsStoreRankMapper;
import com.ruoyi.statistics.service.ISysStatisticsStoreRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class SysStatisticsStoreRankServiceImpl implements ISysStatisticsStoreRankService {

    @Autowired
    SysStatisticsStoreRankMapper sysStatisticsStoreRankMapper;

    @Override
    public List<StoreRank> allStoreRank() {

        List<Long> storeIds = sysStatisticsStoreRankMapper.findAllStoreId();
        List<StoreRank> storeRanks = new ArrayList<>(storeIds.size()); // 初始化大小

        int i = 1;
        // 填充storeRanks
        for (Long id : storeIds) {
            StoreRank storeRank = new StoreRank();
            storeRank.setId(id);
            storeRank.setRank(i);
            storeRank.setStoreName(sysStatisticsStoreRankMapper.findStoreName(id));

            BigDecimal rechargeAmount = sysStatisticsStoreRankMapper.findRechargeAmount(id);
            BigDecimal spendAmount = sysStatisticsStoreRankMapper.findSpendAmount(id);

            storeRank.setRechargeAmount(rechargeAmount != null ? rechargeAmount : BigDecimal.ZERO);
            storeRank.setSpendAmount(spendAmount != null ? spendAmount : BigDecimal.ZERO);

            i++;
            storeRanks.add(storeRank); // 添加到列表中
        }
        return storeRanks;
    }


    @Override
    public List<StoreRank> storeRank(Long deptId) {
        List<Long> storeIds = sysStatisticsStoreRankMapper.findStoreId(deptId);
        List<StoreRank> storeRanks = new ArrayList<>(storeIds.size()); // 初始化大小

        int i = 1;
        // 填充storeRanks
        for (Long id : storeIds) {
            StoreRank storeRank = new StoreRank();
            storeRank.setId(id);
            storeRank.setRank(i);
            storeRank.setStoreName(sysStatisticsStoreRankMapper.findStoreName(id));

            BigDecimal rechargeAmount = sysStatisticsStoreRankMapper.findRechargeAmount(id);
            BigDecimal spendAmount = sysStatisticsStoreRankMapper.findSpendAmount(id);

            storeRank.setRechargeAmount(rechargeAmount != null ? rechargeAmount : BigDecimal.ZERO);
            storeRank.setSpendAmount(spendAmount != null ? spendAmount : BigDecimal.ZERO);

            i++;
            storeRanks.add(storeRank); // 添加到列表中
        }
        return storeRanks;
    }

}
